﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Security.Cryptography;

namespace OnTheWay.Tool
{
    /// <summary>
    /// 定义页面验证帮助类
    /// </summary>
    public class ValidateHelper
    {
        private static Regex m_RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex m_RegEmail = new Regex(@"^[\w-]+@[\w-]+\.(com|net|org|edu|mil|tv|biz|info)$");
        private static Regex m_RegNumber = new Regex("^[0-9]+$");
        private static Regex m_RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
        /// <summary>
        /// 对给定字符串中的特殊字符解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Decode(string str)
        {
            str = str.Replace("<br>", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }
        /// <summary>
        /// 对给定字符串中的特殊字符编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Encode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }
        /// <summary>
        /// 处理并获取输入字符串
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string InputText(string inputString, int maxLength)
        {
            StringBuilder sb = new StringBuilder();
            if ((inputString != null) && (inputString != String.Empty))
            {
                inputString = inputString.Trim();
                if (inputString.Length > maxLength)
                {
                    inputString = inputString.Substring(0, maxLength);
                }
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '<':
                            {
                                sb.Append("&lt;");
                                continue;
                            }
                        case '>':
                            {
                                sb.Append("&gt;");
                                continue;
                            }
                        case '"':
                            {
                                sb.Append("&quot;");
                                continue;
                            }
                    }
                    sb.Append(inputString[i]);
                }
                sb.Replace("'", " ");
            }
            return sb.ToString();
        }
        /// <summary>
        /// 验证给定字符串是否为有效数值
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            return m_RegDecimal.Match(inputData).Success;
        }
        /// <summary>
        /// 验证给定字符串是否为有效Email地址
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            return m_RegEmail.Match(inputData).Success;
        }
        /// <summary>
        /// 验证给定字符串是否为有效数字
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            return m_RegNumber.Match(inputData).Success;
        }
        /// <summary>
        /// 验证给定字符串是否为有效电话号码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsPhone(string inputData)
        {
            return m_RegPhone.Match(inputData).Success;
        }

        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns>
        /// 加密字符串，如果input为null，将返回null
        /// </returns>
        public static string GetMD5Hash(String input)
        {
            if (input == null) return null;

            MD5 md5 = new MD5CryptoServiceProvider();//创建MD5对象  
            byte[] data = System.Text.Encoding.Default.GetBytes(input);//将字符编码为一个字节序列  
            byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值  
            md5.Clear();//清空MD5对象  
            string str = "";//用它记录密码  
            for (int i = 0; i < md5data.Length; i++)
            {
                str += md5data[i].ToString("x").PadLeft(2, '0');
            }

            return str;
        }

        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns>
        /// 加密字符串，如果input为null，将返回null
        /// </returns>
        public static string GetMD5Hash(byte[] input)
        {
            if (input == null) return null;

            MD5 md5 = new MD5CryptoServiceProvider();//创建MD5对象  
            byte[] md5data = md5.ComputeHash(input);//计算data字节数组的哈希值  
            md5.Clear();//清空MD5对象  
            string str = "";//用它记录密码  
            for (int i = 0; i < md5data.Length; i++)
            {
                str += md5data[i].ToString("x").PadLeft(2, '0');
            }

            return str;
        }
        /// <summary>
        /// 双重md5加密
        /// </summary>
        /// <param name="oldpassword"></param>
        /// <returns></returns>
        public static string MD5Password(string oldpassword)
        {
            return GetMD5Hash(GetMD5Hash(oldpassword));
        }

        ///// <summary>
        ///// HTML编码
        ///// </summary>
        ///// <param name="inputData"></param>
        ///// <returns></returns>
        //public static string HtmlEncode(string inputData)
        //{
        //    return HttpUtility.HtmlEncode(inputData);
        //}
        ///// <summary>
        ///// 设置Label控件的值
        ///// </summary>
        ///// <param name="lbl"></param>
        ///// <param name="inputObj"></param>
        //public static void SetLabel(Label lbl, object inputObj)
        //{
        //    SetLabel(lbl, inputObj.ToString());
        //}

        //public static void SetLabel(Label lbl, string txtInput)
        //{
        //    lbl.Text = HtmlEncode(txtInput);
        //}
    }
}
